import { useCallback } from "react";

export type ComponentUseOptions = {
  /** image全局选项 */
  px2rem?: (px: number) => string;
  [key: string]: any;
};

export const BbtReactComponent: ComponentUseOptions = {};
export function useGlobalOption(options: ComponentUseOptions) {
  if (options) {
    BbtReactComponent.options = options;
  }
}

export const useRem = (px2rem?: (px: number) => string) => {
  return useCallback(
    (px) => {
      if (px == 0) return "0";
      if (!px) return "auto";
      return typeof px2rem === "function"
        ? px2rem(px)
        : typeof BbtReactComponent.options?.px2rem === "function"
        ? BbtReactComponent.options?.px2rem(px)
        : px;
    },
    [px2rem]
  );
};
